﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using AmkeniCrossLoginApplication.Data_Access;

namespace Amkeni_System.CO.Edit
{
    public partial class AddOutput : System.Web.UI.Page
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            /*if (User.IsInRole("IP"))
            {
                MasterPageFile = "~/MasterPages/Project.Master";
            }
            if (User.IsInRole("Programme"))
            {
                MasterPageFile = "~/MasterPages/Programme.Master";
            }
            if (User.IsInRole("Administrator"))
            {
                MasterPageFile = "~/MasterPages/CO.Master";
            }
            if (User.IsInRole("IP") && User.IsInRole("Programme"))
            {
                MasterPageFile = "~/MasterPages/ProgProject.Master";
            }*/
        }

        Amkeni_DatabaseEntities db = new Amkeni_DatabaseEntities();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            try
            {
                string ID = OutputIDTextBox.Text.Trim();

                var AcpdOutput = (from c in db.programme_output where c.output_id == ID select c).FirstOrDefault();

                if (AcpdOutput == null)
                {
                    //==================================+
                    //  Add a new CPD Output Record     |
                    //==================================+
                    programme_output newCPD = new programme_output();
                    newCPD.output_id = ID;
                    newCPD.outcome_id = int.Parse(OutComeList.SelectedValue.Trim());
                    newCPD.name = OutputTextBox.Text.Trim();

                    db.programme_output.Add(newCPD);
                    db.SaveChanges();

                    error.Text = "SAVED";
                    error.Visible = true;

                    OutputIDTextBox.Text = null;
                    OutputTextBox.Text = null;
                    OutComeList.SelectedValue = null;
                }
                else
                {
                    error.Text = "That Programme Output ID already exists";
                    error.Visible = true;
                }
            }
            catch (Exception ex)
            {
                error.Text = "Database Error: " + ex.Message + "<br /> " + ex.StackTrace;
                error.Visible = true;
            }
        }

        protected void CancelButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Amkeni_System/CO/Output_List.aspx", true);
        }
    }
}